package com.orange.off.cloud.cloudoff.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.app.MyApplication;
import com.orange.off.cloud.cloudoff.model.UploadResponse;
import com.orange.off.cloud.cloudoff.urls.AppUrls;
import com.orange.off.cloud.cloudoff.utils.JsonUtils;
import com.orange.off.cloud.cloudoff.utils.ProgressDlgUtil;
import com.orange.off.cloud.cloudoff.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * 个人中心------我的佣金---详情
 *
 * @author qs
 */
public class PersonalCommissionDetailFragment extends Fragment {

    @Bind(R.id.commission_detail_title)
    TextView commissionDetailTitle;
    @Bind(R.id.visit_yingjie)//来访
            TextView visitYingjie;
    @Bind(R.id.visit_yijie)
    TextView visitYijie;
    @Bind(R.id.visit_weijie)
    TextView visitWeijie;
    @Bind(R.id.chips_yingjie)//认筹
            TextView chipsYingjie;
    @Bind(R.id.chips_yijie)
    TextView chipsYijie;
    @Bind(R.id.chips_weijie)
    TextView chipsWeijie;
    @Bind(R.id.subscription_yingjie)//认购
            TextView subscriptionYingjie;
    @Bind(R.id.subscription_yijie)
    TextView subscriptionYijie;
    @Bind(R.id.subscription_weijie)
    TextView subscriptionWeijie;
    @Bind(R.id.signed_yingjie)
    TextView signedYingjie;//签约
    @Bind(R.id.signed_yijie)
    TextView signedYijie;
    @Bind(R.id.signed_weijie)
    TextView signedWeijie;
    @Bind(R.id.total_yingjie)
    TextView totalYingjie;//合计
    @Bind(R.id.total_yijie)
    TextView totalYijie;
    @Bind(R.id.total_weijie)
    TextView totalWeijie;
    @Bind(R.id.rt_commissionBack)
    RelativeLayout rtCommissionBack;
    String project_id = "";

    public static PersonalCommissionDetailFragment newInstance(Bundle bundle) {
        PersonalCommissionDetailFragment fragment = new PersonalCommissionDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        getDetailData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mycommissionfragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    private void initViews() {
        project_id = getArguments().getString("id");
        commissionDetailTitle.setText(getArguments().getString("name"));
    }

    /**
     * 获取我的佣金单个项目详情
     */
    void getDetailData() {
        ProgressDlgUtil.showProgressDlg("数据请求中,请稍后...", getActivity());
        RequestParams params = new RequestParams();
        params.addFormDataPart("aid", MyApplication.aid);
        params.addFormDataPart("xid", project_id);
        HttpRequest.post(AppUrls.Commission, params, new BaseHttpRequestCallback<UploadResponse>() {
            @Override
            public void onSuccess(UploadResponse uploadResponse) {
                super.onSuccess(uploadResponse);
                ProgressDlgUtil.stopProgressDlg();
                if (uploadResponse.getSuccess()) {
                    try {
                        JSONObject jsonObject = new JSONObject(uploadResponse.getData());
                        //------------------------------来访-------------------------------------
                        String visit = JsonUtils.getString(jsonObject, "lf", "");
                        JSONObject visitJsonObject = new JSONObject(visit);
                        visitYingjie.setText(JsonUtils.getString(visitJsonObject, "ym", "0"));
                        visitYijie.setText(JsonUtils.getString(visitJsonObject, "hm", "0"));
                        visitWeijie.setText(JsonUtils.getString(visitJsonObject, "wm", "0"));
                        //------------------------------认筹-------------------------------------
                        String chips = JsonUtils.getString(jsonObject, "rc", "");
                        JSONObject chipsJsonObject = new JSONObject(chips);
                        chipsYingjie.setText(JsonUtils.getString(chipsJsonObject, "ym", "0"));
                        chipsYijie.setText(JsonUtils.getString(chipsJsonObject, "hm", "0"));
                        chipsWeijie.setText(JsonUtils.getString(chipsJsonObject, "wm", "0"));
                        //------------------------------认购-------------------------------------
                        String subscription = JsonUtils.getString(jsonObject, "rg", "");//
                        JSONObject subscriptionJsonObject = new JSONObject(subscription);
                        subscriptionYingjie.setText(JsonUtils.getString(subscriptionJsonObject, "ym", "0"));
                        subscriptionYijie.setText(JsonUtils.getString(subscriptionJsonObject, "hm", "0"));
                        subscriptionWeijie.setText(JsonUtils.getString(subscriptionJsonObject, "wm", "0"));
                        //------------------------------签约-------------------------------------
                        String signed = JsonUtils.getString(jsonObject, "qy", "");//
                        JSONObject signedJsonObject = new JSONObject(signed);
                        signedYingjie.setText(JsonUtils.getString(signedJsonObject, "ym", "0"));
                        signedYijie.setText(JsonUtils.getString(signedJsonObject, "hm", "0"));
                        signedWeijie.setText(JsonUtils.getString(signedJsonObject, "wm", "0"));
                        //------------------------------合计-------------------------------------
                        String total = JsonUtils.getString(jsonObject, "zong", "");//
                        JSONObject totalJsonObject = new JSONObject(total);
                        totalYingjie.setText(JsonUtils.getString(totalJsonObject, "ym", "0"));
                        totalYijie.setText(JsonUtils.getString(totalJsonObject, "hm", "0"));
                        totalWeijie.setText(JsonUtils.getString(totalJsonObject, "wm", "0"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                } else {
                    ToastUtils.show(getActivity(), uploadResponse.getMessage(), Toast.LENGTH_SHORT);
                }

            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
                ProgressDlgUtil.stopProgressDlg();
            }

            @Override
            public void onProgress(int progress, long networkSpeed, boolean done) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.rt_commissionBack)
    public void onClick() {
        getActivity().finish();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            getActivity().finish();
            return true;
        }
        return false;
    }


}
